package us.mooresr.geartrack.client.core.desktop;

import java.util.Arrays;
import java.util.List;

import us.mooresr.geartrack.client.core.presenter.MainPresenter;
import us.mooresr.geartrack.client.core.presenter.MainPresenter.MyView;

import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.ViewImpl;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.NoSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.inject.Inject;

public class DesktopMainViewImpl extends ViewImpl implements MainPresenter.MyView {

	private final Widget widget;
	private MainPresenter presenter;
	
	@UiField FlowPanel contentPanel;
	@UiField HTMLPanel buttonPanel;

	public interface Binder extends UiBinder<Widget, DesktopMainViewImpl> {
	}
	
	private static final List<String> menuChoices = Arrays.asList(
			"Borrow",
			"Return",
			"Items",
			"People",
			"Item Types",
			"Locations");
	
	private static final List<String> tokens = Arrays.asList(
			"!borrow",
			"!return",
			"!items",
			"!people",
			"!itemtypes",
			"!locations");

	@Inject
	public DesktopMainViewImpl(final Binder binder) {
		
		widget = binder.createAndBindUi(this);
		
		TextCell cell = new TextCell();
		final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
		CellList<String> cellList = new CellList<String>(cell);
		
		cellList.setRowData(menuChoices);
		cellList.setSelectionModel(selectionModel);
		
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				String selected = selectionModel.getSelectedObject();
				
				if (selected != null) {
					
					int which = menuChoices.indexOf(selected);
					
					String target = tokens.get(which);
					
					presenter.goTo(target);
				}
				
			}
			
		});
		
		buttonPanel.add(cellList);
	}

	@Override
	public Widget asWidget() {
		return widget;
	}
	
	@Override
	public void setInSlot(Object slot, Widget content) {
		if (slot == MainPresenter.TYPE_SetMainContent) {
			setMainContent(content);
		} else {
			super.setInSlot(slot, content);
		}
	}
	
	private void setMainContent(Widget content) {
		contentPanel.clear();
		
		if (content != null) {
			contentPanel.add(content);
		}
		
	}

	@Override
	public void setPresenter(MainPresenter presenter) {
		this.presenter = presenter;
		
	}
}
